﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace YmtUserAuth.DataImport
{
    public class Mapping
    {
        public Mapping(string db, string tb)
        {
            this.Db = db;
            this.Tb = tb;
        }

        public string Tb { get; private set; }
        public string Db { get; private set; }
    }

    public class UuserCredentialMappingTable
    {
        /// <summary>
        /// 根据用户凭据分表
        /// </summary>
        /// <param name="uc">原始登录凭据</param>
        /// <returns></returns>
        public static Mapping MappingTable(string uc)
        {
            MappingTable mt_mail = new MappingTableMail();
            MappingTable mt_tel = new MappingTableTel();
            MappingTable mt_loginid = new MappingTableLogid();
            mt_loginid.SetMappingTableDuties(mt_tel);
            mt_tel.SetMappingTableDuties(mt_mail);
            //从Mapping下一个职责为空的对象开始执行
            return mt_loginid.Mapping(uc);
        }
    }

    internal abstract class MappingTable
    {
        protected const string defTable = "ULC_L_201402";
        protected const string mailTable = "ULC_E_201402";
        protected const string logidTable = "ULC_L_201402";
        protected const string telTable = "ULC_T_201402";

        protected MappingTable mt;

        public void SetMappingTableDuties(MappingTable mt)
        {
            this.mt = mt;
        }
        public abstract Mapping Mapping(string uc);
    }

    internal class MappingTableMail : MappingTable
    {
        //public MappingTableMail(MappingTable mt) : base(mt) { }
        public override Mapping Mapping(string uc)
        {
            var patten = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            var r = new Regex(patten, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (r.IsMatch(uc))
                return new Mapping(Configure.MongoDatabaseName, mailTable);
            else if (mt != null)
                return mt.Mapping(uc);
            else return new Mapping(Configure.MongoDatabaseName, defTable);
        }
    }

    internal class MappingTableTel : MappingTable
    {
        //public MappingTableTel(MappingTable mt) : base(mt) { }
        public override Mapping Mapping(string uc)
        {
            var patten = @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$";
            var r = new Regex(patten, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (r.IsMatch(uc))
                return new Mapping(Configure.MongoDatabaseName, telTable);
            else if (mt != null)
                return mt.Mapping(uc);
            else return new Mapping(Configure.MongoDatabaseName, defTable);
        }
    }

    internal class MappingTableLogid : MappingTable
    {
        //public MappingTableLogid(MappingTableTel mt) : base(mt) { }
        public override Mapping Mapping(string uc)
        {
            if (mt != null)
                return mt.Mapping(uc);
            else return new Mapping(Configure.MongoDatabaseName, defTable);
        }
    }
}
